package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.Stat.StatFactory;
import cn.wit.summit.chwan.interfaces.AdapterInterface;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.IntentDataBean;
import cn.wit.summit.chwan.models.dto.JumpBaseBean;
import cn.wit.summit.chwan.models.response.ThematicListResponse;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.widgets.ItemListView;

/**
 * Created by Alan on 2018/4/4.
 */

public class ThematicListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Activity mContext;
    private LayoutInflater inflater;

    private Map<String, String> downTemp = new HashMap<>();

    public ThematicListAdapter(Activity context, List<ViewBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(mContext);
    }

    public void clearTempData () {
        downTemp.clear();
    }

    public String getPoByGameId(String gameId) {
        String result = "";
        if (downTemp.size() > 0 && downTemp.containsKey(gameId)) {
            result = downTemp.get(gameId);
        }
        return result;
    }

    public GameListBaseBean getBeanData(int itemPo, int po) {
        if (mDatas.get(itemPo).getViewType() == ThematicListAdapter.ViewType.TYPE_GAME) {
            GameListBaseBean list = (GameListBaseBean) mDatas.get(itemPo).getObj();
            return list;
        }
        return null;
    }

    public enum ViewType {
        TYPE_TOP_IMAGE,
        TYPE_LINE,
        TYPE_LINE_SMALL,
        TYPE_TITLE,
        TYPE_GAME
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_TITLE.ordinal()) {
            holder = new TitleHolder(inflater.inflate(R.layout.thematic_title_item, parent, false));
        } else if (viewType == ViewType.TYPE_GAME.ordinal()) {
            holder = new GameHolder(inflater.inflate(R.layout.find_fragment_game_item_item, parent, false));
        } else if (viewType == ViewType.TYPE_LINE.ordinal()) {
            holder = new LineHolder(inflater.inflate(R.layout.item_game_starter_line_view, parent, false));
        } else if (viewType == ViewType.TYPE_LINE_SMALL.ordinal()) {
            holder = new LineSmallHolder(inflater.inflate(R.layout.item_daily_new_small_line_view, parent, false));
        } else if (viewType == ViewType.TYPE_TOP_IMAGE.ordinal()) {
            holder = new TopImageHolder(inflater.inflate(R.layout.item_daily_new_top_image_view, parent, false));
        }

        return holder;
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        ViewBean viewBean = mDatas.get(position);
        if (holder instanceof LineHolder) {
        } else if (holder instanceof LineSmallHolder) {
        } else if (holder instanceof TopImageHolder) {
            TopImageHolder topImageHolder = (TopImageHolder) holder;
            final String temp = (String) viewBean.getObj();
            topImageHolder.itemTopIv.setImageURI(temp);
        } else if (holder instanceof GameHolder) {
            GameHolder GameHolder = (GameHolder) holder;
            GameListBaseBean gameListBaseBean = (GameListBaseBean) viewBean.getObj();
            GameHolder.listItem.setData(mContext, gameListBaseBean);
            downTemp.put(gameListBaseBean.getGame_id(), position + "");
        } else if (holder instanceof TitleHolder) {
            TitleHolder titleHolder = (TitleHolder) holder;
            final ThematicListResponse thematicListResponse = (ThematicListResponse) viewBean.getObj();
            if (thematicListResponse == null) return;
            titleHolder.title.setText(thematicListResponse.getCollection_title());
            titleHolder.moreText.setText("全部");
            titleHolder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentDataBean bean = new IntentDataBean();
                    bean.setJump_link_type(2);
                    bean.setJump_link_type_val(thematicListResponse.getCollection_id());
                    IntentUtil.getInstance().intentTo(mContext, bean);
                }
            });
        }
    }

    class LineHolder extends RecyclerView.ViewHolder {

        public LineHolder(View view) {
            super(view);
        }
    }

    class TopImageHolder extends RecyclerView.ViewHolder {

        public SimpleDraweeView itemTopIv;

        public TopImageHolder(View view) {
            super(view);
            itemTopIv = view.findViewById(R.id.itemTopIv);
        }
    }

    class LineSmallHolder extends RecyclerView.ViewHolder {

        public LineSmallHolder(View view) {
            super(view);
        }
    }

    public ItemListView listItem;


    private class GameHolder extends RecyclerView.ViewHolder {
        public ItemListView listItem;

        public GameHolder(View inflate) {
            super(inflate);
            listItem = inflate.findViewById(R.id.listItem);
        }
    }

    class TitleHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView moreText;
        View     more;

        public TitleHolder(View inflate) {
            super(inflate);
            title = inflate.findViewById(R.id.title);
            moreText = inflate.findViewById(R.id.moreText);
            more = inflate.findViewById(R.id.more);
            if (more.getVisibility() == View.GONE) {
                more.setVisibility(View.VISIBLE);
            }
        }
    }

    public class ListHolder extends RecyclerView.ViewHolder {
        ItemListView listItem;

        public ListHolder(View view) {
            super(view);
            listItem = view.findViewById(R.id.listItem);
        }
    }
}
